package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.mapper.OutboundMapper;
import com.guigu.youxuan.pojo.*;
import com.guigu.youxuan.pojo.dto.OutboundDto;
import com.guigu.youxuan.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class OutboundServiceImpl extends ServiceImpl<OutboundMapper, Outbound> implements IOutboundService {
    @Autowired
    private IWarehouseDetailsService iWarehouseDetailsService;
    @Autowired
    private IWarehouseService iWarehouseService;
    @Autowired
    private IOutboundDetailService outboundDetailService;
    @Autowired
    private IGoodsInfoService iGoodsInfoService;
    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private OutboundMapper outboundMapper;
    /**
     * 出库列表查询所有出库信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Outbound> OutStorageListPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Outbound> goodsInfos=this.list();
        PageInfo<Outbound> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    /**
     * 出库审核查询状态为S001-1待审核的信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Outbound> OutStorageFuhePage(int currentPage, int pageSize) {
        QueryWrapper<Outbound> wrapper=new QueryWrapper<>();
        wrapper.eq("check_tag","S001-1");
        PageHelper.startPage(currentPage,pageSize);
        List<Outbound> goodsInfos=this.list(wrapper);
        PageInfo<Outbound> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }
    /**
     * 出库审核通过
     * @param outboundDto
     */
    @Transactional
    @Override
    public void YesFuheReviewList(OutboundDto outboundDto) {
        String orderList = outboundDto.getOutbound().getOrderList();
        List<Long> orderIdList = new ArrayList<>();

        String[] orderArray = orderList.split(",");
        for (String orderId : orderArray) {
            orderIdList.add(Long.parseLong(orderId));
        }
        for(Long l : orderIdList) {
            OrderInfo byId = iOrderInfoService.getById(l);
            byId.setConveyTag("P001-3");
            iOrderInfoService.updateById(byId);
        }
        //第一个对象用dto里的gather
        Outbound outbound =outboundDto.getOutbound();
        //给审核时间设置时间
        outbound.setCheckTime(new Date());
        //给复核标志改为 审核通过
        outbound.setCheckTag("S001-3");
        this.updateById(outbound);
        //第二个对象用dto里的gatherDetails
        List<OutboundDetail> list=outboundDto.getOutboundDetails();
        //for循环赋值给gatherDetails
        for (OutboundDetail outboundDetail :list){
            //如果有这条数据就把入库明细表里的数据修改 另外减当前库存数量
            WarehouseDetails byId = iWarehouseDetailsService.getById(outboundDetail.getWarehouseDetailsId());
            byId.setNowAmount(byId.getNowAmount()-outboundDetail.getAmount());
            iWarehouseDetailsService.updateById(byId);
            //new仓库对象 用仓库service.getByid查入库表里面的仓库id
            Warehouse iWarehouseServiceById = iWarehouseService.getById(byId.getWarehouseId());
            iWarehouseServiceById.setNowAmount(iWarehouseServiceById.getNowAmount()-outboundDetail.getAmount());
            iWarehouseService.updateById(iWarehouseServiceById);
            GoodsInfo iGoodsInfoServiceById = iGoodsInfoService.getById(outboundDetail.getGoodsId());
            iGoodsInfoServiceById.setStockUp(iGoodsInfoServiceById.getStockUp()-outboundDetail.getAmount());

            if (iGoodsInfoServiceById.getStockUp()==0){
                iGoodsInfoServiceById.setStatus(0);
            }
            iGoodsInfoService.updateById(iGoodsInfoServiceById);
        }
    }

    @Override
    public String getMaxDesignId() {
        Outbound outbound = outboundMapper.getMaxDesignId();
        if(outbound == null){
            return null;
        }
        // 获取设计单编号
        String designId = outbound.getPayOrderNumber();
        return designId;
    }

    @Transactional
    @Override
    public void createOutbound(Outbound outbound, List<OutboundDetail> outboundDetailList) {
        this.save(outbound);
        for(OutboundDetail o : outboundDetailList){
            OutboundDetail outboundDetail = new OutboundDetail();
            outboundDetail.setPayId(outbound.getPayId());
            outboundDetail.setWarehouseDetailsId(o.getWarehouseDetailsId());
            outboundDetail.setGoodsId(o.getGoodsId());
            outboundDetail.setAmount(o.getAmount());
            outboundDetail.setPrice(o.getPrice());
            outboundDetail.setSubtotal(o.getSubtotal());
            outboundDetailService.save(outboundDetail);
        }
    }


}
